import { plainToInstance } from "class-transformer";
import axios from "axios";
import ExcelJs from "@scvzerng/exceljs";
import {
  BluePrint,
  PlanetSchematics,
  UniverseCategory,
  UniverseConstellation,
  UniverseRegion,
  UniverseStation,
  UniverseStruct,
  UniverseSystem,
  UniverseType,
  UniverseTypeMaterials,
} from "../entity";
import { FileUtils } from "../../utils";

import {
  DATA_URL,
  FORMATED_BLUEPRINTS_FILE,
  FORMATED_CATEGORY_FILE,
  FORMATED_PLANET_SCHEMATICS_FILE,
  FORMATED_TYPE_FILE,
  FORMATED_TYPE_MATERIALS_FILE,
} from "../contanst";

export class SDEReader {
  /**
   * 读取物品类型。
   */
  static readUniverseType(): UniverseType[] {
    return plainToInstance(UniverseType, FileUtils.readJSON<UniverseType>(FORMATED_TYPE_FILE));
  }

  /**
   * 读取物品的组成成分
   */
  static readUniverseTypeMaterials(): UniverseTypeMaterials[] {
    return plainToInstance(
      UniverseTypeMaterials,
      FileUtils.readJSON<UniverseTypeMaterials>(FORMATED_TYPE_MATERIALS_FILE)
    );
  }

  /**
   * 读取蓝图信息
   */
  static readBlueprints(): BluePrint[] {
    return plainToInstance(BluePrint, FileUtils.readJSON<BluePrint>(FORMATED_BLUEPRINTS_FILE));
  }

  /**
   * 读取行星产物成分
   */
  static readPlantSchematics(): PlanetSchematics[] {
    return plainToInstance(PlanetSchematics, FileUtils.readJSON<PlanetSchematics>(FORMATED_PLANET_SCHEMATICS_FILE));
  }

  /**
   * 读取物品分类
   */
  static readCategories(): UniverseCategory[] {
    return plainToInstance(UniverseCategory, FileUtils.readJSON<UniverseCategory>(FORMATED_CATEGORY_FILE)).map((e) => {
      e.children.forEach((c) => {
        c.parent = e;
      });
      return e;
    });
  }
  /**
   * 读取星域相关信息
   */
  static async readUniverseRegions() {
    const response = await axios({
      method: "get",
      url: DATA_URL,
      responseType: "arraybuffer",
    });
    const instance = new ExcelJs.Workbook();
    const workbook = await instance.xlsx.load(response.data);
    const regionWorksheet = workbook.getWorksheet("星域列表");
    const constellationWorksheet = workbook.getWorksheet("星座列表");
    const systemWorksheet = workbook.getWorksheet("星系列表");
    const npcStationWorksheet = workbook.getWorksheet("NPC空间站");
    // 玩家公开建筑
    const playerStructWorksheet = workbook.worksheets.find((sheet) => sheet.name.startsWith("玩家公开建筑"));
    const regions = UniverseRegion.fromSheet(regionWorksheet!);

    const constellations = UniverseConstellation.fromSheet(constellationWorksheet!, regions);
    constellations.forEach((constellation) => {
      constellation.region = undefined;
    });
    const systems = UniverseSystem.fromSheet(systemWorksheet!, constellations);
    systems.forEach((system) => {
      system.constellation = undefined;
    });
    UniverseStation.fromSheet(npcStationWorksheet!, systems).forEach((station) => {
      station.system = undefined;
    });
    UniverseStruct.fromSheet(playerStructWorksheet!, systems).forEach((struct) => {
      struct.system = undefined;
    });
    return regions;
  }
}
